package Servlet.Student;

import Entity.Grade.FinalGrade;
import Entity.Process.Report;
import Entity.Process.ReportDetail;
import Entity.ReplyGroup;
import Service.Student.Imp.StudentPaperServiceImp;
import Service.Student.StudentPaperService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/submitReport", "/getScore", "/getReportDetail", "/getStuDefence"})
public class StudentPaperServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/submitReport".equals(url)) {
            submitReport(request, response);
        } else if ("/getReportDetail".equals(url)) {
            getReportDetail(request, response);
        } else if ("/getScore".equals(url)) {
            getScore(request, response);
        } else if ("/getStuDefence".equals(url)) {
            getStuDefence(request, response);
        }
    }

    /**
     * 根据学号获取答辩组信息
     */
    private void getStuDefence(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StudentPaperService studentPaperService = new StudentPaperServiceImp();
        String sno = request.getParameter("sno");
        ReplyGroup replyGroup = studentPaperService.getStuDefence(sno);
        String json = JSONObject.toJSONString(replyGroup, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 获取评分信息
     */
    private void getScore(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StudentPaperService studentPaperService = new StudentPaperServiceImp();
        String sno = request.getParameter("sno");
        FinalGrade finalGrade = studentPaperService.getScore(sno);
        String json = JSONObject.toJSONString(finalGrade, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();

    }


    /**
     * 根据reportId查询开题报告详情
     */
    private void getReportDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StudentPaperService studentPaperService = new StudentPaperServiceImp();
        Integer reportId = Integer.parseInt(request.getParameter("reportId"));
        ReportDetail reportDetail = studentPaperService.getReportDetail(reportId);
        String json = JSONObject.toJSONString(reportDetail, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


    /**
     * 提交开题报告
     */
    private void submitReport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StudentPaperService studentPaperService = new StudentPaperServiceImp();
        String NO = request.getParameter("userName");
        String data = request.getParameter("taskData");
        JSONObject json = JSON.parseObject(data);
        Report report = new Report();
        ReportDetail reportDetail = new ReportDetail();
        report.setSno(NO);
        report.setTitle(json.getString("title"));
        report.setCollName(json.getString("collegeName"));
        report.setMName(json.getString("major"));
        report.setTno(json.getString("tno"));
        reportDetail.setSence(json.getString("sense"));
        reportDetail.setDevelop(json.getString("trend"));
        reportDetail.setContent(json.getString("report"));
        reportDetail.setScheme(json.getString("scheme"));
        reportDetail.setAim(json.getString("aim"));
        reportDetail.setDatum(json.getString("literature"));
        boolean flag = studentPaperService.submitReport(report, reportDetail);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }
}
